<?php
/**
 * User zgh
 * Email 1803897171@qq.com
 * Date 2021/7/20
 **/

namespace app\admin\service;


use app\common\Annotation\ControllerAnnotation;
use app\common\Annotation\NodeAnotation;
use app\common\model\SystemNodeModel;
use app\common\service\AuthService;
use Doctrine\Common\Annotations\AnnotationReader;

use think\facade\Env;
use think\Db;

class NodeService
{

    const TYPE_NO_LOGIN = 0;
    const TYPE_LOGON = 1;
    const TYPE_AUTH = 2;

    /**
     * 获取所有的节点 包括需要授权   不需要授权的
     */
    public static function getNodelist()
    {
        $module_arr = [['path' => 'admin/controller', 'module' => "admin", 'replace' => ['controller', 'Controller']]]; //绑定的module
        $app_path = Env::get('app_path');
        $nodeList = [];
        foreach ($module_arr as $module_row) {
            $path = $app_path . $module_row['path'] . "/";
            $controllerList = AuthService::getControllerList($path);
            if (!empty($controllerList)) {
                $reader = new AnnotationReader();
                foreach ($controllerList as $controllerName) {
                    $reflectionClass = new \ReflectionClass($controllerName);
                    $controllerAnnotation = $reader->getClassAnnotation($reflectionClass, ControllerAnnotation::class);
                    $controllerTitle = !empty($controllerAnnotation) && !empty($controllerAnnotation->title) ? $controllerAnnotation->title : null;
                    $controllerAuth = !empty($controllerAnnotation) && !empty($controllerAnnotation->auth) ? $controllerAnnotation->auth : 1;
                    $methods = $reflectionClass->getMethods();
                    $actionList = []; //所有的方法
                    $tmp = explode("\\", $reflectionClass->name);
                    foreach ($methods as $method) {
                        // 读取NodeAnotation的注解
                        $nodeAnnotation = $reader->getMethodAnnotation($method, NodeAnotation::class);
                        if (!empty($nodeAnnotation) && !empty($nodeAnnotation->title)) {
                            $actionTitle = $nodeAnnotation->title;
                            $actionAuth = $nodeAnnotation->auth ?: $controllerAuth;
                            if ($actionAuth == self::TYPE_AUTH) {
                                $actionList[] = [
                                    'node' => AuthService::parseNodeStr($module_row['module'] . "." . (str_replace($module_row['replace'], '', end($tmp)) . "/" . $method->name)),
                                    'title' => $actionTitle,
                                    'is_auth' => $actionAuth,
                                    'type' => 2,
                                ];
                            }
                        }
                    }
                    if (!empty($actionList)) {
                        $nodeList[] = [
                            'node' => AuthService::parseNodeStr($module_row['module'] . "." . (str_replace($module_row['replace'], '', end($tmp)))),
                            'title' => $controllerTitle,
                            'is_auth' => $controllerAuth,
                            'type' => 1,
                        ];
                        $nodeList = array_merge($nodeList, $actionList);
                    }
                }
            }
        }
        return $nodeList;
    }

    /**
     * 更新节点
     */
    public static function refresh($params)
    {
        $nodeList = self::getNodelist();
        Db::startTrans();
        $res = SystemNodeModel::where([["type", ">", 0]])->delete();
        $res2 = true;
        if (!empty($nodeList)) {
            $model = new SystemNodeModel();
            $res2 = $model->saveAll($nodeList);
        }
        if ($res !== false && $res2 !== false) {
            Db::commit();
            return true;
        } else {
            Db::rollback();
            return false;
        }
    }

}